let User = require('../model/user')
let Describe = require('../model/describe')
let result = require('../utils/result')
let format = require('../utils/format.js')
function DescribeController(){

};


/**
 * 获取自我介绍
 * @param req
 * @param res
 * @returns {Promise<void>}
 */
DescribeController.prototype.getDescribe = async (req,res)=>{
    let uid = req.body.uid;
    if(!uid){
        res.json(result(-1,'请求参数不合法'))
        return;
    }
    let describe = await Describe.findOne({where: {uid:uid,isDel: 0}});
    res.status(200).json(result(0,'success',describe))
}


/**
 * 添加自我介绍
 * @param req
 * @param res
 * @returns {Promise<void>}
 */
DescribeController.prototype.addDescribe = async (req,res)=>{
    let uid = req.body.uid;
    let des= req.body.des;
    if(!uid){
        res.json(result(-1,'请求参数不合法'))
        return;
    };
    Describe.findOne({where:{uid:uid}}).then(rsp=>{
        if(!rsp){
            Describe.create({des:req.body.des,uid:req.body.uid}).then(describe=>{
                res.status(200).json(result(0,'success',describe))
            });

        }else{
            Describe.update({des:req.body.des},{where: {uid:uid,isDel: 0}}).then(describe=>{
                res.status(200).json(result(0,'success',describe))
            });
        }
    }).catch(err=>{
        res.status(200).json(result(-1,'失败'))
    })

}




let describeController = new DescribeController();
module.exports = describeController
